package com.teacher.pc.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.teacher.common.CommonResult;
import com.teacher.mapper.TermMapper;
import com.teacher.pojo.dto.fourprinciple.FourPrincipleAddDto;
import com.teacher.pojo.dto.fourprinciple.FourPrincipleGetDto;
import com.teacher.pojo.dto.fourprinciple.FourPrincipleUpdateDto;
import com.teacher.pojo.entity.FourPrinciple;
import com.teacher.pojo.vo.fourprinciple.FourPrincipleGetVo;
import com.teacher.service.IFourPrincipleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 四项基本原则言行 前端控制器
 * </p>
 *
 * @author zfm
 * @since 2021-08-02
 */
@RestController
@RequestMapping("/pc/four-principle")
@Api(tags = "四项基本原则言行")
public class FourPrincipleController {
    @Autowired
    private IFourPrincipleService iFourPrincipleService;

    @Autowired
    private TermMapper termMapper;

    /**
     * 添加四项基本原则言行
     *
     * @param dto
     * @return
     */
    @ApiOperation("添加四项基本原则言行 fourPrinciple_add")
    @PostMapping
    @PreAuthorize("hasAuthority('fourPrinciple_add')")
    public CommonResult<String> add(@RequestBody @Validated FourPrincipleAddDto dto) {
        FourPrinciple fourPrinciple = new FourPrinciple();
        dto.setTermId(termMapper.getCurrentTerm());
        BeanUtils.copyProperties(dto,fourPrinciple);
        if (iFourPrincipleService.save(fourPrinciple)) {
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    /**
     * 更新四项基本原则言行
     *
     * @param dto
     * @return
     */
    @ApiOperation("更新四项基本原则言行 fourPrinciple_update")
    @PutMapping
    @PreAuthorize("hasAuthority('fourPrinciple_update')")
    public CommonResult<String> update(@RequestBody @Validated FourPrincipleUpdateDto dto) {
        FourPrinciple fourPrinciple = new FourPrinciple();
        BeanUtils.copyProperties(dto, fourPrinciple);
        if (iFourPrincipleService.updateById(fourPrinciple)) {
            return CommonResult.success("更新成功");
        }
        return CommonResult.failed("更新失败");
    }

    /**
     * 删除四项基本原则言行
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除四项基本原则言行 fourPrinciple_delete")
    @PreAuthorize("hasAuthority('fourPrinciple_delete')")
    public CommonResult<String> del(@RequestBody List<Long> ids) {
        if (iFourPrincipleService.removeByIds(ids)) {
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("删除失败");
    }

    /**
     * 查询四项基本原则言行
     *
     * @param dto
     * @return
     */
    @GetMapping
    @ApiOperation("查询四项基本原则言行 fourPrinciple_get")
    @PreAuthorize("hasAuthority('fourPrinciple_get')")
    public CommonResult<IPage<FourPrincipleGetVo>> getPage(FourPrincipleGetDto dto) {
        return iFourPrincipleService.getPage(dto);
    }

    /**
     * 导入四项基本原则言行
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("导入四项基本原则言行 fourPrinciple_upload")
    @PreAuthorize("hasAuthority('fourPrinciple_upload')")
    public CommonResult<String> upload(@RequestParam(name = "file") MultipartFile file) {
        return iFourPrincipleService.upload(file);
    }
}

